package cn.itsource.basic.service.impl;

import cn.itsource.basic.mapper.BaseMapper;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.BaseService;
import cn.itsource.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

//@Service 不需要交给spring管理
public class BaseServiceImpl<T> implements BaseService<T> {
    @Autowired
    private BaseMapper<T> mapper;

    @Override
    public List<T> findAll() {
        return mapper.findAll();
    }

    @Override
    public T findById(Long id) {
        return mapper.findById(id);
    }

    @Override
    public void add(T t) {
        mapper.add(t);
    }

    @Override
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    public void delete(Long id) {
        mapper.delete(id);
    }

    @Override
    public PageList<T> queryByPage(BaseQuery query) {
        Integer totals = mapper.queryByCount(query);
        if (0 >= totals){
            return new PageList<>(0,new ArrayList<>());
        }
        List<T> rows = mapper.queryByPage(query);
        return new PageList<>(totals,rows);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        mapper.batchDelete(ids);
    }
}
